//
//  TypeAllTopicModel.m
//  LiWuShuo
//
//  Created by 袁小航 on 14/11/18.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TypeAllTopicModel.h"
#import "AFAppDotNetAPIClient.h"

@implementation TypeAllTopicModel

-(instancetype)initWithAllTopicAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.allTopicId = [[attributes objectForKey:@"id"] integerValue];
    self.allTopicImage = [attributes objectForKey:@"cover_image_url"];
    self.allTopicTitle = [attributes objectForKey:@"title"];
    self.allTopicSubTitle = [attributes objectForKey:@"subtitle"];
    
    return self;
}

+ (NSURLSessionDataTask *)allTopicWithBlock:(void (^)(NSArray *, NSError *))block
{
    return [[AFAppDotNetAPIClient sharedClient] GET:@"http://api.liwushuo.com/v1/collections?limit=12&offset=0" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *postsFromResponse = [[responseObject objectForKey:@"data"] objectForKey:@"collections"];
        
        NSMutableArray * mutablePosts = [NSMutableArray array];
        for (NSDictionary *attributes in postsFromResponse) {
            TypeAllTopicModel *model = [[TypeAllTopicModel alloc]initWithAllTopicAttributes:attributes];
            [mutablePosts addObject:model];
        }
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (block) {
            block([NSArray array], nil);
        }
    }];
}


@end
